package com.coolbi.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 工具类 - 公用
 */

public class CommonUtil {
	
	public static final String PATH_PREPARED_STATEMENT_UUID = "\\{uuid\\}";// UUID路径占位符
	public static final String PATH_PREPARED_STATEMENT_DATE = "\\{date(\\(\\w+\\))?\\}";// 日期路径占位符
	
	/**
	 * 获取WebRoot路径
	 * 
	 * @return WebRoot路径
	 */
	public static String getWebRootPath() {
		return "";
	}

	/**
	 * 随机获取UUID字符串(无中划线)
	 * 
	 * @return UUID字符串
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
//		return uuid.substring(0, 8) + uuid.substring(9, 13) + uuid.substring(14, 18) + uuid.substring(19, 23) + uuid.substring(24);
		return uuid.replace("-", "");
	}
	
	/**
	 * 获取实际路径
	 * 
	 * @param path
	 *            路径
	 */
	public static String getPreparedStatementPath(String path) {
		if (StringUtils.isEmpty(path)) {
			return null;
		}
		StringBuffer uuidStringBuffer = new StringBuffer();
		Matcher uuidMatcher = Pattern.compile(PATH_PREPARED_STATEMENT_UUID).matcher(path);
		while(uuidMatcher.find()) {
			uuidMatcher.appendReplacement(uuidStringBuffer, CommonUtil.getUUID());
		}
		uuidMatcher.appendTail(uuidStringBuffer);
		
		StringBuffer dateStringBuffer = new StringBuffer();
		Matcher dateMatcher = Pattern.compile(PATH_PREPARED_STATEMENT_DATE).matcher(uuidStringBuffer.toString());
		while(dateMatcher.find()) {
			String dateFormate = "yyyyMM";
			Matcher dateFormatMatcher = Pattern.compile("\\(\\w+\\)").matcher(dateMatcher.group());
			if (dateFormatMatcher.find()) {
				String dateFormatMatcherGroup = dateFormatMatcher.group();
				dateFormate = dateFormatMatcherGroup.substring(1, dateFormatMatcherGroup.length() - 1);
			}
			dateMatcher.appendReplacement(dateStringBuffer, new SimpleDateFormat(dateFormate).format(new Date()));
		}
		dateMatcher.appendTail(dateStringBuffer);
		
		return dateStringBuffer.toString();
	}
	
	/**
	 * 将字符串按分隔符转换为List
	 * @param src
	 * @param patten
	 * @return
	 */
	public static List<Long> parseToList(String src, String patten) {
		List<Long> list = new ArrayList<Long>();
		String[] groupId = src.split(patten);
		for (int i = 0; i < groupId.length; i ++) {
			list.add(Long.parseLong(groupId[i]));
		}
		return list;
	}
	
	/**
	 * 将数组转换为List
	 * @param src
	 * @return
	 */
	public static List<Long> parseToList(Long[] src) {
		List<Long> list = new ArrayList<Long>();
		for (int i = 0; i < src.length; i ++) {
			list.add(src[i]);
		}
		return list;
	}
	
	/**
	 * 
	 * 将字符串按规则转换为double数组
	 * @param src 源字符串
	 * @param patten 分割规则
	 * @return
	 */
	public static double[] parseToDouble(String src, String patten) {
		String[] array = src.split(patten);
		double[] result = new double[array.length];
		for (int i = 0; i < array.length; i ++) {
			result[i] = Double.parseDouble(array[i]);
		}
		return result;
	}
	
	/**
	 * 
	 * 将字符串按规则转换为long数组
	 * @param src 源字符串
	 * @param patten 分割规则
	 * @return
	 */
	public static long[] parseToLong(String src, String patten) {
		String[] array = src.split(patten);
		long[] result = new long[array.length];
		for (int i = 0; i < array.length; i ++) {
			result[i] = Long.parseLong(array[i]);
		}
		return result;
	}
	
}